﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using FlickrNet;

namespace FlickrPaper.master
{
    public partial class FlickerPaper : System.Web.UI.MasterPage
    {
        private string set = ConfigurationManager.AppSettings["FlickrDefaultset"].ToString();
        private string tags =ConfigurationManager.AppSettings["FlickrDefaulttags"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                imgBG.ImageUrl = ConfigurationManager.AppSettings["flickrDefault"].ToString();
                if (ConfigurationManager.AppSettings["flickrRandom"] == "true")
                {
                    Flickr flickr = new Flickr(
                        ConfigurationManager.AppSettings["FlickrApiKey"].ToString(),
                        ConfigurationManager.AppSettings["FlickrSharedSecret"].ToString());

                    //----------------------------------------------------------------------------------------
                    // Use this Block of code to access private photos
                    //----------------------------------------------------------------------------------------
                    //Flickr flickr = new Flickr(
                    //    ConfigurationManager.AppSettings["FlickrApiKey"].ToString(),
                    //    ConfigurationManager.AppSettings["FlickrSharedSecret"].ToString(),
                    //    ConfigurationManager.AppSettings["FlickrToken"].ToString());
                    //Auth auth = new Auth();
                    //auth.Permissions = AuthLevel.Write;
                    //auth.Token = ConfigurationManager.AppSettings["FlickrToken"].ToString();
                    //auth.User = new FoundUser();
                    //auth.User.FullName = ConfigurationManager.AppSettings["FlickrFullName"].ToString();
                    //auth.User.UserId = ConfigurationManager.AppSettings["FlickrUserID"].ToString();
                    //auth.User.UserName = ConfigurationManager.AppSettings["FlickrUserName"].ToString();
                    //flickr.AuthToken = auth.Token;
                    //----------------------------------------------------------------------------------------

                    PhotoCollection webpaper = new PhotoCollection();

                    if (Request.QueryString["set"] != "" && Request.QueryString["set"] != null)
                        set = Request.QueryString["set"];
                    if (Request.QueryString["tags"] != "" && Request.QueryString["tags"] != null)
                        tags = Request.QueryString["tags"];


                    if (tags != "")
                    {
                        PhotoSearchOptions searchOptions = new PhotoSearchOptions("me");
                        searchOptions.Tags = tags;
                        searchOptions.PerPage = 1000;
                        webpaper = flickr.PhotosSearch(searchOptions);
                    }
                    else
                    {
                        PhotosetPhotoCollection tempset = flickr.PhotosetsGetPhotos(set);
                        foreach (Photo tempphoto in tempset)
                        {
                            webpaper.Add(tempphoto);
                        }
                    }

                    int itemCount = webpaper.Count;
                    Random randomNumber = new Random();
                    int index = randomNumber.Next(itemCount - 1);
                    imgBG.ImageUrl = webpaper.ElementAt(index).LargeUrl.ToString();
                }
            }
        }


    }
}